// Attempt to log in with phone and password.
//
// @param selector {String|Object} One of the following:
//   - {phone: (phone)}
// @param password {String}
// @param callback {Function(error|undefined)}

/**
 * @summary Log the user in with a password. Indian phone numbers are accepted without 91. For others, the country code is required. Uses https://github.com/halt-hammerzeit/libphonenumber-js, as dynamic import
 * @locus Server
 * @param {String}  phone phone number
 * @return {String} sanitized phone number. Tweaked for Indian numbers, but works for other countries as well.
 */
Accounts.sanitizePhone =  function(phone) {
	check(phone, String);
	if (!phone) return null;
	else return phone;
};

/**
 * @summary Log the user in with a password.
 * @locus Client
 * @param {Object} options phone, otp and optional expectedUserId
 * @param {Function} [callback] Optional callback. Called with no arguments on success,
 *      or with a single `Error` argument on failure.
 * @return {Void} null
 */
Meteor.loginWithPhone = async function(options, callback) {
	check(options, {phone: String, otp: String, purpose: Match.Maybe(String), expectedUserId: Match.Optional(Match.Maybe(String))});

	options.phone =  Accounts.sanitizePhone(options.phone);
	if (!options.phone) {
		callback && callback(new Meteor.Error(404, 'Please provide a valid phone number for sending otp'));
		return;
	}

	Accounts.callLoginMethod({
		methodArguments: [options],
		userCallback: callback
	});
};
